package com.kl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kl.domain.*;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.Collection;
import com.kl.feign.CreatOrderServiceFeign;
import com.kl.feign.OrderServcieFeign;
import com.kl.mapper.CollectionMapper;
import com.kl.mapper.DetailedMapper;
import com.kl.service.ICollectionService;
import com.kl.service.IDetailedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/collectionController")
@Api(value = "代收货款管理接口")
public class CollectionController1 {

    @Autowired
    private OrderServcieFeign orderServcieFeign;

    @Autowired
    private DetailedMapper detailedMapper;

    @Autowired
    private IDetailedService detailedService;

    @Autowired
    private ICollectionService collectionService;

    @Autowired
    private CreatOrderServiceFeign detailedServiceFeign;

    @Autowired
    private CollectionMapper collectionMapper;

    @ApiOperation(value = "根据用户id增加代收货款")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "waybillId", value = "waybillId", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "status", required = true, paramType = "query"),
    })
    @PostMapping
    public Result insertCollection(@RequestParam String waybillId,@RequestParam String status){


            //通过订单id 查询 运单id

        //运单实体类
        Result<WaybillDTO> result1 = orderServcieFeign.selectById(Long.valueOf(waybillId));
        if(result1.getCode().equals(1007)){
            System.out.println("运单熔断");
            return Result.build(ConstantEnum.FaillBack);
        }
        WaybillDTO waybillDTO =result1.getResult();
        if(status.equals("1")){
            //正常到货
            Collection collection = new Collection();//正常到货
            //明细实体类
            Long waybillId1 = waybillDTO.getWaybillId();
            System.out.println(waybillId1);
            Result<List<DetailedDTO>> detailById = detailedServiceFeign.findDetailById(waybillId1);
            if(detailById.getCode().equals(1007)){
                System.out.println("detai0熔断");
                return Result.build(ConstantEnum.FaillBack);
            }
            DetailedDTO detailedDTO = detailById.getResult().get(0);

            //set 运单id
            collection.setWid(waybillDTO.getWaybillId());
            //set 订单id
            collection.setOid(waybillDTO.getOrderId());
            collection.setDate(new Date());
            //手续费
            BigDecimal bigDecimal = new BigDecimal(detailedDTO.getTotalPrice().intValue() * 0.1);
            collection.setServiceCharge(bigDecimal);
            //set 代收货款的金额
            BigDecimal bigDecimal1 = new BigDecimal(detailedDTO.getTotalPrice().doubleValue());
            collection.setMoney(bigDecimal1.subtract(bigDecimal));
            collection.setRefundType("1");
            collection.setPaymentStatus(0);
            // TODO 用户id
            System.out.println(collection);
            // TODO 理赔
//            collectionMapper.insert(collection);
        }else if (status.equals("2")){
            Collection collection = new Collection();//代收货款实体类
            collection.setDate(new Date());

            collection.setPaymentStatus(0);
            //
//            Result<List<DetailedDTO>> detailById = detailedServiceFeign.findDetailById(waybillDTO.getWaybillId());
//            if(detailById.getCode().equals(1007)){
//                System.out.println("detai2熔断");
//
//                return Result.build(ConstantEnum.FaillBack);
//            }
//            BigDecimal totalPrice =detailById.getResult().get(0).getTotalPrice();
            //set 运单id
            collection.setWid(waybillDTO.getWaybillId());
            //set 订单id
            collection.setOid(waybillDTO.getOrderId());
            //set 退款类型
            collection.setRefundType("3");
            //退款金额
            Result<OrderDTO> orderById = orderServcieFeign.findOrderById(waybillDTO.getOrderId());
            if(orderById.getCode().equals(1007)){
                System.out.println("orderServcieFeign熔断");

                return Result.build(ConstantEnum.FaillBack);

            }
            OrderDTO result = orderById.getResult();
            collection.setMoney(result.getCargoInsurancePrice());
            System.out.println(collection);
            collectionMapper.insert(collection);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    @ApiOperation(value = "根据用户id查询代收货款")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "signReceipt", value = "signReceipt", required = true, paramType = "query"),
    })
    @GetMapping
    public Result<List<CollectionDTO>> selectCollectionAll(@RequestParam String signReceipt){
        Page<Collection> page = new Page<>(1, 5);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("sign_receipt",signReceipt);
//        List<Collection> list = collectionMapper.selectList(wrapper);
//        String s = JSON.toJSONString(list);
//        List<Collection> collections = JSONObject.parseArray(s, Collection.class);
        IPage<Collection> page1 = collectionMapper.selectPage(page, wrapper);
        List<Collection> list = page1.getRecords();

        List<CollectionDTO> collections = JSONObject.parseArray(JSON.toJSONString(list), CollectionDTO.class);

        return Result.build(ConstantEnum.SUCCESS).setResult(collections);
    }

    @ApiOperation(value = "修改代收货款代收货款")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "cid", required = true, paramType = "query"),
            @ApiImplicitParam(name = "paymentStatus", value = "paymentStatus", required = true, paramType = "query"),

    })
    @PutMapping
    public Result updateCollectionAll(@RequestParam Long cid,@RequestParam Integer paymentStatus){

        Collection collection = new Collection();
        collection.setPaymentStatus(paymentStatus);
        collection.setCid(cid);
        collection.updateById();
        return Result.build(ConstantEnum.SUCCESS);
    }





}
